package ubi.quando.UI;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private UbiApplication mApp;
	private Button mBtnLogin;
	private Button mBtnRegister;
	private EditText mTxtMail;
	private EditText mTxtPwd;
	private TextView mTxt1;
	private TextView mTxt2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		mApp = (UbiApplication) getApplication();
		mApp.setCurrentActivity(this);

		mBtnLogin = (Button) findViewById(R.id.btn_login);
		mBtnRegister = (Button) findViewById(R.id.btn_register);
		mTxtMail = (EditText) findViewById(R.id.txt_mail);
		mTxtPwd = (EditText) findViewById(R.id.txt_pwd);

		mTxt1 = (TextView) findViewById(R.id.font1);
		mTxt2 = (TextView) findViewById(R.id.font2); 
		initFont(); // set custom font for all text object in xml file 
		
		mBtnLogin.setOnClickListener(new LoginOnClickListener());
		mBtnRegister.setOnClickListener(new RegisterOnClickListener());
	}
	
	private void initFont() {
		Typeface font = Typeface.createFromAsset(getAssets(), "AGENCYR.TTF");  
		mTxt1.setTypeface(font);
		mTxt2.setTypeface(font);
		mBtnLogin.setTypeface(font);
		mBtnRegister.setTypeface(font);
		mTxtMail.setTypeface(font);
		mTxtPwd.setTypeface(font);
	}

	public class LoginOnClickListener implements OnClickListener {
		public void onClick(View v) {
			String mail = mTxtMail.getText().toString();
			String pwd = mTxtPwd.getText().toString();

			mApp.setMail(mail);
			mApp.setPwd(pwd);
			if(mApp.setUser(null))
			{
				Intent mainMenu = new Intent(getApplicationContext(), MainMenu.class);

				startActivity(mainMenu); 
			}
			else {
				Toast.makeText(getApplicationContext(),
						"Mail or password are not recognized", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	public class RegisterOnClickListener implements OnClickListener {
		public void onClick(View v) {
			Intent register = new Intent(v.getContext(), RegisterActivity.class);

			startActivity(register);
		}
	}
}
